#include <limits>
#include <cmath>

#include <gmpxx.h>

#include "segment.h"

segment::segment(): a(point()), b(point())
{
}

segment::segment(const point& a, const point& b): a(a), b(b)
{
}

double segment::get_y(double x) const
{
    double y = a.y + (x - a.x) * (b.y - a.y) / (b.x - a.x);

    const double meps = std::numeric_limits<double>::epsilon() / 2;
    double eps = 8 * meps * (std::abs(a.y) + std::abs((x - a.x) * (b.y - a.y) / (b.x - a.x)));

    if (std::abs(y - std::round(y)) < eps)
        return get_y_gmp(x);
    else
        return y;
}

double segment::get_y_gmp(double x) const
{
    mpf_class y = mpf_class(a.y) + (mpf_class(x) - mpf_class(a.x)) * (mpf_class(b.y) - mpf_class(a.y)) / (mpf_class(b.x) - mpf_class(a.x));

    return y.get_d();
}
